package com.study.springboottest.poi;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class PoiTest1 {

    public static void main(String[] args) throws IOException {
        // 1. 创建工作簿
        Workbook workbook = new XSSFWorkbook();
        // 2. 创建表单
        Sheet sheet = workbook.createSheet("test1");
        Row row = sheet.createRow(2);
        Cell cell = row.createCell(2);
        cell.setCellValue("测试POI");

        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderTop(BorderStyle.THIN);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);

        Font font = workbook.createFont();
        font.setFontName("微软雅黑");
        cellStyle.setFont(font);

        row.setHeightInPoints(31);
        sheet.setColumnWidth(2, 31 * 256);

        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        cell.setCellStyle(cellStyle);


        FileInputStream fileInputStream = new FileInputStream("E:\\code\\springboot-study\\logo.jpg");
        byte[] bytes = IOUtils.toByteArray(fileInputStream);
        int index = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
        CreationHelper helper = workbook.getCreationHelper();
        Drawing<?> patriarch = sheet.createDrawingPatriarch();
        ClientAnchor clientAnchor = helper.createClientAnchor();
        clientAnchor.setRow1(0);
        clientAnchor.setCol1(0);
        Picture picture = patriarch.createPicture(clientAnchor, index);
        picture.resize();


        // 3. 文件流
        FileOutputStream outputStream = new FileOutputStream("E:\\code\\springboot-study\\test1.xlsx");
        // 4. 写入文件
        workbook.write(outputStream);
        fileInputStream.close();
        outputStream.close();
    }
}
